package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.dao.BasePayItemBgtEcoDao;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.BaseBgtEco;
import com.open.capacity.reimburse.dao.BaseBgtEcoDao;
import com.open.capacity.reimburse.service.BaseBgtEcoService;


@Service
public class BaseBgtEcoServiceImpl implements BaseBgtEcoService {

    @Autowired
    private BaseBgtEcoDao baseBgtEcoDao;

    @Autowired
    private BasePayItemBgtEcoDao payItemBgtEcoDao;

    /**
     * 添加
     *
     * @param baseBgtEco
     */
    public int save(BaseBgtEco baseBgtEco) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        baseBgtEco.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        baseBgtEco.setStatus(1);
        baseBgtEco.setUpdateUserid(loginUser.getId());
        baseBgtEco.setUpdateUsername(loginUser.getRealName());
        baseBgtEco.setUpdateTime(new Date());
        return baseBgtEcoDao.save(baseBgtEco);
    }

    /**
     * 修改
     *
     * @param baseBgtEco
     */
    public int update(BaseBgtEco baseBgtEco) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        baseBgtEco.setUpdateUserid(loginUser.getId());
        baseBgtEco.setUpdateUsername(loginUser.getRealName());
        baseBgtEco.setUpdateTime(new Date());
        return baseBgtEcoDao.update(baseBgtEco);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return baseBgtEcoDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BaseBgtEco findById(String id) {
        return baseBgtEcoDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BaseBgtEco> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BaseBgtEco> list = baseBgtEcoDao.findAll(params);
        PageInfo<BaseBgtEco> pageInfo = new PageInfo(list);

        return PageResult.<BaseBgtEco>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BaseBgtEco> findAll(Map<String, Object> params) {
        return baseBgtEcoDao.findAll(params);
    }
}
